<?php
namespace app\index\controller;

use app\index\model\User;
use app\index\model\Agency;
use app\index\model\BuyRecord;
use app\index\model\Push as Pushs;
class Buyers extends Comm
{
    /*
     * 买家数据查询
     * 返回买家数据
     */
    public function buyersindex(){
        $user = new User;
        $Buysers = $user->showBuysers();
        /*foreach ($Buysers as $value) {//计算优惠券数量-注释掉
          echo count(explode(',',$value['ticketid']));
        }*/
        //echo  count(explode(',','u.ticketid'));die;
        //$array2 = $user->DisplayArea($Buysers);

        return view('index',['buyer'=>$Buysers]);
    }
      /**
     * 全球逆地理编码调用
     */
    function showjwd(){
        $id  = input('id');
        $jd  = input('jd');
        $wd  = input('wd');
        $jwd = $wd.','.$jd;
        $results = nibm($jwd);
        echo $jwd.'|'.$results;
    }
    public function buyRecord(){
        $br = new BuyRecord;
        $buyrecord = $br->buyrecord();
        return view('buyRecord',['buyrecord'=>$buyrecord]);
    }
    /**
     * 买家添加页面显示
     */
    public function add(){
        if(isset($_POST['submit'])){
           $name      = input('name');
           $intergral = input('intergral');
           $money     = input('money');
           $iphone    = input('iphone');
           $email     = input('email');
           $account   = input('account');
           $root      = input('root');
           $path     = input('locat');
           $hjwd      = input('hjwd');

           $user = new User;
           $results = $user->useradd($name,$intergral,$money,$iphone,$email,$account,$root,$path,$hjwd);
           if($user){
                 zsgc('添加了会员['.$name.']的信息');
                 $this->success('操作成功','index/buyers/Buyersindex');
             }
             else{
                 $this->error('操作失败','index/buyers/Buyersindex');
             }
          }
        return view('add');
    }

    public function deldo(){
        $id = trim(input('id'));
        $Buyers = User::get($id);
        $name = $Buyers['name']; 
        if(!empty($id))
        {
            $re=User::destroy($id);
            if($re==false){
                show(0,'删除失败');
            }else{
                zsgc('删除了会员['.$name.']的信息');
                show(1,'删除成功');
            }
        }
        else
        {
            $this->error('操作失败','index/buyers/Buyersindex');
        }
    }
    public function mod(){
      if(isset($_POST['submit'])){
         $id        = input('hid');
         $name      = input('name');
         $intergral = input('intergral');
         $money     = input('money');
         $iphone    = input('iphone');
         $email     = input('email');
         $path     = input('locat');
         $hjwd      = input('hjwd');
         $user = new User;
         $results = $user->usermod($id,$name,$intergral,$money,$iphone,$email,$path,$hjwd);
 
         if($results){
              zsgc('修改了会员['.$name.']的信息');
              $this->success('操作成功','index/buyers/buyersindex');
         }
         else{
             $this->error('操作失败','index/buyers/buyersindex');
         }

      }else{
        $id = input('id');
        $user = User::get($id)->toArray();


        return view('mod',['user'=>$user]);
      }
        
    }
    //显示买家买东西返积分的详情
    public function amount(){
        $pushs=new Pushs();
        $res=$pushs->AllData(['id'=>4]);
        return view('amount',['res'=>$res]);
    }
    //修改返还积分的数量
    public function upamount(){
        $pushs=new Pushs();
        if(isset($_POST['sub'])){
            $res=$pushs->save(['amount'=>$_POST['amount']],['id'=>$_POST['id']]);
            if($res)
                $this->success('修改成功！','buyers/amount');
            else
                $this->error('修改失败！','buyers/amount');
        }
        $res=$pushs->AllData(['id'=>$_GET['id']]);
        return view('upamount',['res'=>$res]);
    }
}
